package com.dubbo.config;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;

/**
 * Created by Administrator on 2020/6/28 0028.
 */
// @Activate 无条件自动激活
@Activate
@Slf4j
public class DubboLogFilter implements Filter{
    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        String name = invoker.getInterface().getName();
        Object[] args = invocation.getArguments();
        String method = invocation.getMethodName();
        String prefix = "日志：" + name + "." + method;
        log.info(prefix + " 入参=>" + JSONArray.toJSONString(args));
        Result rs = null;
        try {
            String clientIp = RpcContext.getContext().getRemoteHost();
            rs = invoker.invoke(invocation);
            if (rs.hasException()) {
                Throwable e = rs.getException();
                log.info("异常：" + JSONObject.toJSONString(e));
            }
            log.info("远程地址ip ========》："+clientIp);
        }catch (Exception e) {
            // TODO: handle exception
        }

        return rs;
    }
}
